package edu.webtech.project.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import edu.webtech.project.client.UserAccountService;
import edu.webtech.project.client.UserAccountServiceAsync;
import edu.webtech.project.shared.UserInfoDTO;

public class LoginPage extends Composite {

	private static LoginPageUiBinder uiBinder = GWT
			.create(LoginPageUiBinder.class);
	

	interface LoginPageUiBinder extends UiBinder<Widget, LoginPage> {
	}
	
	private final UserAccountServiceAsync userAccountService = GWT
			.create(UserAccountService.class);

	private MainLayout mainLayout;
	private String     strUserName;
	private String     strPassword;
	
	@UiField Button  btnSignIn;
	@UiField Button  btnRegister;
	@UiField TextBox txtUserName;
	@UiField TextBox txtUserPass;
	@UiField Label   lblUserName;
	@UiField Label   lblUserPass;
	@UiField SimplePanel tablePanel;

	public LoginPage(MainLayout o) {
		initWidget(uiBinder.createAndBindUi(this));
		mainLayout = o;
		
		tablePanel.add(new SearchPanel());
	}
	
	public void updateFromForm() {
		this.strUserName = txtUserName.getText().trim();
		this.strPassword = txtUserPass.getText().trim();
	}
	
	public int Validate() {
		
		lblUserName.setText("");
		lblUserPass.setText("");
		
		if (true == strUserName.isEmpty()) {
			lblUserName.setText("Please enter a username.");
			return 1;
		}
			
		if (true == strPassword.isEmpty()) {
			lblUserPass.setText("Please enter a password.");
			return 2;
		}		
		
		return 0;
	}

		
	public void Authenticate() {
		
		userAccountService.getUser(strUserName, new AsyncCallback<UserInfoDTO>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Shame !! Some error occurred.");
			}

			@Override
			public void onSuccess(UserInfoDTO result) {
				
				if (result != null && strPassword.compareToIgnoreCase(result.getUserPass()) == 0) {
					mainLayout.getContentPanel().clear();
					mainLayout.getContentPanel().add(new ProfilePage(mainLayout));
				} else {
					lblUserPass.setText("Username, password doesn't match.");
				}
			}
			
		});
		
	}
	
	@UiHandler("btnSignIn")
	void onNewAccountClick(ClickEvent e) {
		
		updateFromForm();
		if (0 == Validate()) {
			Authenticate();
		}
		
	}
	
	@UiHandler("txtUserPass")
	void onPassKeyDown(KeyDownEvent e) {
		switch (e.getNativeEvent().getKeyCode()) {

		case KeyCodes.KEY_ENTER:
			updateFromForm();
			if (0 == Validate()) {
				Authenticate();
			}
			break;
		}
	}
	
	@UiHandler("btnRegister")
	void onRegisterClick(ClickEvent e) {
		mainLayout.getContentPanel().clear();
		mainLayout.getContentPanel().add(new RegisterPage(mainLayout));
	}
	
	

}
